package com.medicalsystem.controller;

import com.medicalsystem.entity.Prescription;
import com.medicalsystem.service.PrescriptionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/prescriptions")
@Tag(name = "处方管理", description = "提供处方信息的增删改查功能")
public class PrescriptionController {

    private final PrescriptionService prescriptionService;

    public PrescriptionController(PrescriptionService prescriptionService) {
        this.prescriptionService = prescriptionService;
    }

    @Operation(summary = "获取所有处方信息", description = "返回所有处方信息列表")
    @GetMapping
    public List<Prescription> getAllPrescriptions() {
        return prescriptionService.getAllPrescriptions();
    }

    @Operation(summary = "根据ID获取处方信息", description = "通过处方ID获取处方详细信息")
    @GetMapping("/{id}")
    public ResponseEntity<Prescription> getPrescriptionById(@PathVariable Long id) {
        Prescription prescription = prescriptionService.getPrescriptionById(id);
        return prescription != null ? ResponseEntity.ok(prescription) : ResponseEntity.notFound().build();
    }

    @Operation(summary = "新增处方信息", description = "保存处方信息到数据库")
    @PostMapping
    public Prescription savePrescription(@RequestBody Prescription prescription) {
        return prescriptionService.savePrescription(prescription);
    }

    @Operation(summary = "修改处方信息", description = "根据ID修改处方详细信息")
    @PutMapping("/{id}")
    public ResponseEntity<Prescription> updatePrescription(@PathVariable Long id, @RequestBody Prescription prescription) {
        Prescription updatedPrescription = prescriptionService.updatePrescription(id, prescription);
        return updatedPrescription != null ? ResponseEntity.ok(updatedPrescription) : ResponseEntity.notFound().build();
    }

    @Operation(summary = "删除处方信息", description = "根据ID删除处方信息")
    @DeleteMapping("/{id}")
    public ResponseEntity<Void> deletePrescription(@PathVariable Long id) {
        prescriptionService.deletePrescription(id);
        return ResponseEntity.noContent().build();
    }
}
